<template>
  <div class="employees">
    <el-card class="btn">
      <div>
        <el-button type="primary" @click="goImport">导入</el-button>
        <el-button type="primary" @click="add">+新增员工</el-button>
      </div>
    </el-card>
    <el-card>
      <el-table :data="list">
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="头像" prop="username">
          <template v-slot="{ row }">
            <img
              v-imgerror="errorUrl"
              :src="row.staffPhoto"
              alt=""
              class="avatar"
              @click="imgClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <el-table-column label="聘用形式" prop="formOfEmployment">
          <template v-slot="{ row }">
            <!-- <div v-hireType="row.formOfEmployment" /> -->
            <div>{{ +row.formOfEmployment | filterHireType }}</div>
          </template>
        </el-table-column>
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="入职时间" prop="timeOfEntry">
          <template v-slot="{ row }">
            <div>
              {{ row.timeOfEntry | filterTime }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="是否在职"></el-table-column> -->
        <el-table-column label="状态" prop="enableState" />
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="godetails(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button
                type="text"
                @click="rolesClick(row.id)"
              >角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 5, 10, 100, 200, 300, 400]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 弹框 -->
    <DiaLogItem v-model="show" @getData="getData" />
    <!-- 二维码 -->
    <el-dialog title="二维码" width="400" :visible.sync="showQrcode">
      <canvas ref="canvas" style="text-align: center" />
    </el-dialog>
    <!-- 分配角色弹框 -->
    <DiaLogRoles ref="dialogroles" />
  </div>
</template>

<script>
import Qrcode from 'qrcode'
import DiaLogItem from '@/views/employees/components/dialogItem.vue'
import { sysUser, sysUserDel } from '@/api/employees.js'
import DiaLogRoles from './components/diaRoles.vue'
export default {
  components: {
    DiaLogItem,
    DiaLogRoles
  },
  data() {
    return {
      show: false,
      errorUrl: require('@/assets/common/head.jpg'),
      showQrcode: false,
      page: {
        page: 1,
        size: 10,
        total: 100
      },
      list: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    //  获取员工列表
    async getData() {
      const res = await sysUser(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
      console.log('员工列表', res)
    },
    //  页容量改变时
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    //  页码改变时
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    //  添加员工
    add() {
      this.show = true
    },
    //  导入
    goImport() {
      this.$router.push(
        '/employees/import?name=' +
          encodeURIComponent('员工') +
          `&redirect=${encodeURIComponent('/employees')}`
      )
    },
    //  进入查看页
    godetails(id) {
      console.log('id', id)
      this.$router.push('/employees/details/' + id)
    },
    //  删除
    del(id) {
      this.$confirm('确定要删除吗?', '提示')
        .then(async() => {
          await sysUserDel(id)
          this.$message.success('删除成功')
          this.handleCurrentChange(1)
        })
        .catch(() => {})
    },
    imgClick(imgUrl) {
      if (!imgUrl) {
        this.$message.error('暂无图片')
        return
      }
      this.showQrcode = true

      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, imgUrl, {
          width: 300,
          height: 300
        })
      })
    },
    //  点击角色
    rolesClick(id) {
      this.$refs.dialogroles.isShow = true
      //  点开获得该员工的基本详情
      this.$refs.dialogroles.getuserInfo(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 30px;
  .btn {
    text-align: right;
    margin-bottom: 15px;
  }
  .page {
    text-align: right;
    padding-top: 15px;
  }
}
.avatar {
  width: 30px;
  height: 30px;
  object-fit: contain;
}
</style>
